/**
 * 
 */
package com.puneet.graphqueries.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @author puneet
 *
 */
public class TCPMessageReceiverThread implements Runnable {
	private Socket socket;
	private TCPMessageReceiver receiver;
	
	public TCPMessageReceiverThread(Socket socket,TCPMessageReceiver receiver) {
		this.socket=socket;
		this.receiver = receiver;
	}
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String s;
			StringBuffer messageBuffer = new StringBuffer();
			while((s=br.readLine()) != null) {
				messageBuffer.append(s+"\n");
			}
			messageBuffer.setLength(messageBuffer.length()-1);
			System.out.println("Message received: " + messageBuffer);
			receiver.messageReceived(messageBuffer.toString());
			br.close();
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
